{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "# Preamble to mimick book environment\n",
    "import logging\n",
    "from pprint import pprint\n",
    "from sys import stdout as STDOUT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 1\n",
    "class MyObject(object):\n",
    "    def __init__(self):\n",
    "        self.public_field = 5\n",
    "        self.__private_field = 10\n",
    "\n",
    "    def get_private_field(self):\n",
    "        return self.__private_field"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 2\n",
    "foo = MyObject()\n",
    "assert foo.public_field == 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 3\n",
    "assert foo.get_private_field() == 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'MyObject' object has no attribute '__private_field'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-10-add080dad97b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# Example 4\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mfoo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__private_field\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m: 'MyObject' object has no attribute '__private_field'"
     ]
    }
   ],
   "source": [
    "# Example 4\n",
    "foo.__private_field"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 5\n",
    "class MyOtherObject(object):\n",
    "    def __init__(self):\n",
    "        self.__private_field = 71\n",
    "\n",
    "    @classmethod\n",
    "    def get_private_field_of_instance(cls, instance):\n",
    "        return instance.__private_field\n",
    "\n",
    "bar = MyOtherObject()\n",
    "assert MyOtherObject.get_private_field_of_instance(bar) == 71"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 6\n",
    "try:\n",
    "    class MyParentObject(object):\n",
    "        def __init__(self):\n",
    "            self.__private_field = 71\n",
    "    \n",
    "    class MyChildObject(MyParentObject):\n",
    "        def get_private_field(self):\n",
    "            return self.__private_field\n",
    "    \n",
    "    baz = MyChildObject()\n",
    "    baz.get_private_field()\n",
    "except:\n",
    "    logging.exception('Expected')\n",
    "else:\n",
    "    assert False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 7\n",
    "assert baz._MyParentObject__private_field == 71"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'_MyParentObject__private_field': 71}\n"
     ]
    }
   ],
   "source": [
    "# Example 8\n",
    "print(baz.__dict__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 9\n",
    "class MyClass(object):\n",
    "    def __init__(self, value):\n",
    "        self.__value = value\n",
    "\n",
    "    def get_value(self):\n",
    "        return str(self.__value)\n",
    "\n",
    "foo = MyClass(5)\n",
    "assert foo.get_value() == '5'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 10\n",
    "class MyIntegerSubclass(MyClass):\n",
    "    def get_value(self):\n",
    "        return int(self._MyClass__value)\n",
    "\n",
    "foo = MyIntegerSubclass(5)\n",
    "assert foo.get_value() == 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 11\n",
    "class MyBaseClass(object):\n",
    "    def __init__(self, value):\n",
    "        self.__value = value\n",
    "\n",
    "    def get_value(self):\n",
    "        return self.__value\n",
    "\n",
    "class MyClass(MyBaseClass):\n",
    "    def get_value(self):\n",
    "        return str(super().get_value())\n",
    "\n",
    "class MyIntegerSubclass(MyClass):\n",
    "    def get_value(self):\n",
    "        return int(self._MyClass__value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'MyIntegerSubclass' object has no attribute '_MyClass__value'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-18-264cf171acb8>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# Example 12\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mfoo\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mMyIntegerSubclass\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mfoo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_value\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m<ipython-input-17-08fd295a19f2>\u001b[0m in \u001b[0;36mget_value\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m     13\u001b[0m \u001b[0;32mclass\u001b[0m \u001b[0mMyIntegerSubclass\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mMyClass\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     14\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mget_value\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_MyClass__value\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m: 'MyIntegerSubclass' object has no attribute '_MyClass__value'"
     ]
    }
   ],
   "source": [
    "# Example 12\n",
    "foo = MyIntegerSubclass(5)\n",
    "foo.get_value()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 13\n",
    "class MyClass(object):\n",
    "    def __init__(self, value):\n",
    "        # This stores the user-supplied value for the object.\n",
    "        # It should be coercible to a string. Once assigned for\n",
    "        # the object it should be treated as immutable.\n",
    "        self._value = value\n",
    "\n",
    "    def get_value(self):\n",
    "        return str(self._value)\n",
    "\n",
    "class MyIntegerSubclass(MyClass):\n",
    "    def get_value(self):\n",
    "        return self._value\n",
    "\n",
    "foo = MyIntegerSubclass(5)\n",
    "assert foo.get_value() == 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello and hello should be different\n"
     ]
    }
   ],
   "source": [
    "# Example 14\n",
    "class ApiClass(object):\n",
    "    def __init__(self):\n",
    "        self._value = 5\n",
    "\n",
    "    def get(self):\n",
    "        return self._value\n",
    "\n",
    "class Child(ApiClass):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self._value = 'hello'  # Conflicts\n",
    "\n",
    "a = Child()\n",
    "print(a.get(), 'and', a._value, 'should be different')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5 and hello are different\n"
     ]
    }
   ],
   "source": [
    "# Example 15\n",
    "class ApiClass(object):\n",
    "    def __init__(self):\n",
    "        self.__value = 5\n",
    "\n",
    "    def get(self):\n",
    "        return self.__value\n",
    "\n",
    "class Child(ApiClass):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self._value = 'hello'  # OK!\n",
    "\n",
    "a = Child()\n",
    "print(a.get(), 'and', a._value, 'are different')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
